﻿using Model;
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using Spire.Pdf.Tables;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Util;

namespace WpfApp1
{
    /// <summary>
    /// Alert.xaml 的交互逻辑
    /// </summary>
    public partial class Alert : Window
    {
        public Alert()
        {
            InitializeComponent();
        }


        public List<ExamInee> DataList;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            HandyControl.Controls.Growl.Success("文件保存成功！");

            HandyControl.Controls.Growl.Info("文件保存成功！");

            HandyControl.Controls.Growl.InfoGlobal("文件保存成功！");
            HandyControl.Controls.Growl.Warning("文件保存成功！");

            HandyControl.Controls.Growl.Error("文件保存成功！");
            //global.Success("文件保存成功！");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            //D:\Documents\支付宝开放平台开发助手

            var w= RSAHelper.ReadFile(@"D:\Documents\out.json");

            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            var examIneeList = Serializer.Deserialize<List<ExamInee>>(w);

            this.DataList = examIneeList;

            this.listview.ItemsSource = examIneeList;

            demo();
            //run();
            //biao();



        }

        public string Rand()
        {

            Random ran = new Random();
            int RandKey = ran.Next(1, 160);

            String v = "";
            for(var k = 0; k < RandKey; k++)
            {
                v += "w-";
            }
            return v;
        }

 

        public void demo()
        {


            //创建一个PDF文档
            PdfDocument doc = new PdfDocument();

          

            //指定页面大小
            doc.PageSettings.Size = PdfPageSize.A4;

            //将页边距设置为0 
            doc.PageSettings.Margins = new PdfMargins(0);

            //创建PdfMargins对象，指定期望设置的页边距 
            PdfMargins margins = new PdfMargins(40, 60, 40, 60);

            //在文档模板的顶部和底部应用页眉页脚模板 
            doc.Template.Top = CreateHeaderTemplate(doc, margins);            //doc.Template.Top = CreateHeaderTemplate(doc, margins);
            doc.Template.Bottom = CreateFooterTemplate(doc, margins);

            //在文档模板的左右部分应用空白模板 
            //doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            //doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);

            ////在文档中添加两页并写入文字
            ///
           
            //PdfPageBase page1 = doc.Pages.Add();
            //PdfPageBase page2 = doc.Pages.Add();
            //PdfTrueTypeFont font = new PdfTrueTypeFont(new System.Drawing.Font("微软雅黑", 15f), true);
            //page1.Canvas.DrawString("正文开始的地方...", font, PdfBrushes.Black, 0, 0);
            //page2.Canvas.DrawString("正文开始的地方...", font, PdfBrushes.Black, 0, 0);


            //创建一个PdfTable对象
            PdfTable table = new PdfTable();

            //设置字体
            table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);
            table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 9f), true);

            //创建一个DataTable并写入数据
            //DataTable dataTable = new DataTable();
            //dataTable.Columns.Add("名字");
            //dataTable.Columns.Add("年龄");
            //dataTable.Columns.Add("性别");


            //for (var i = 0; i < 10; i++)
            //{
            //    PdfPageBase page1 = doc.Pages.Add();

            //    for (var i = 0; i < 16; i++)
            //    {
            //        dataTable.Rows.Add(new string[] { "张红", "22", "女" });
            //        dataTable.Rows.Add(new string[] { "王东", "25", "男" });
            //    }
            //}

            var a = new ArrayList();


            for (int i = 0; i < 500; i++)
            {
                a.Add(Rand());
            }

            var height = 842-150;

           

            for (var k=0;k<9;k++)
            {

                PdfPageBase page = doc.Pages.Add();
                

                //创建一个PdfGrid对象
                PdfGrid grid = new PdfGrid();

                //设置单元格边距
                grid.Style.CellPadding = new PdfPaddings(1, 1, 5, 5);

                //添加2行4列

                grid.Columns.Add(4);

                //设置列宽
                foreach (PdfGridColumn col in grid.Columns)
                {
                    col.Width = 60f;
                }

                float pageNowHeight = 0;



                //写入数据
                for (int i = 0; i < 5000; i++)
                {
                    PdfGridRow row = grid.Rows.Add();

                    row.Cells[0].Value = Rand();

                    row.Cells[0].ColumnSpan = 4;

                    if (pageNowHeight+row.Height>height)
                    {
                        grid.Rows.Remove(row);
                        break;

                    }
                    else
                    {
                        pageNowHeight += row.Height;
                    }
                }

                //设置列宽
                foreach (var row in grid.Rows)
                {
                    //row.Height = 40f;
                }
                

                //将表格绘入文档
                grid.Draw(page, new PointF(30, 20));

            }






            //保存文档
            doc.SaveToFile("HeaderAndFooter.pdf");
            System.Diagnostics.Process.Start("HeaderAndFooter.pdf");

        }

        //在自定义事件中设置行高
        private static void Table_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
        {
            args.MinimalHeight = 20f;
        }

        public void run()
        {

            //创建PDF文档
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(0,0,0,0));

            var countPage = 1;

            if (this.DataList.Count % 22 != 0)
            {
                countPage = this.DataList.Count / 22 + 1;
            }
            else
            {
                countPage = this.DataList.Count;
            }
         
            //for

            //指定页面大小
            doc.PageSettings.Size = PdfPageSize.A4;
          
            //将页边距设置为0 
            doc.PageSettings.Margins = new PdfMargins(0);

            //创建PdfMargins对象，指定期望设置的页边距 
            ///PdfMargins margins = new PdfMargins(40, 60, 40, 60);

            ////在文档模板的顶部和底部应用页眉页脚模板 
            doc.Template.Top = CreateHeaderTemplate(doc, new PdfMargins(40, 40, 40, 40));
            doc.Template.Bottom = CreateFooterTemplate(doc, new PdfMargins(40, 10, 40, 40));

            ////在文档模板的左右部分应用空白模板 
            //doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            //doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);




            //考生抽签表


            //自定义PdfTrueTypeFont、PdfPen实例
            //PdfTrueTypeFont font = new PdfTrueTypeFont(new System.Drawing.Font("微软雅黑", 13f), true);
            //PdfPen pen = new PdfPen(System.Drawing.Color.Black);


            //在指定位置绘入表格

            var grid = write(this.DataList);
            
            grid.Draw(page, new System.Drawing.PointF(37.5f, 40));


            var pages= doc.Pages;


            //保存到文档
            doc.SaveToFile("PDF表格.pdf");
            System.Diagnostics.Process.Start("PDF表格.pdf");

        }


        public PdfGrid write(List<ExamInee> tempList)
        {
            //创建一个PdfGrid对象
            PdfGrid grid = new PdfGrid();
            //设置单元格边距和表格默认字体
            grid.Style.CellPadding = new PdfPaddings(1, 1, 5, 5);
            grid.Style.Font = new PdfTrueTypeFont(new System.Drawing.Font("微软雅黑", 12f), true);
            grid.Columns.Add(5);

 

            PdfGridRow row = grid.Rows.Add();
         
            PdfGridRow row4 = grid.Rows.Add();
            row4.Height = 100f;
            row.Style.Font = new PdfTrueTypeFont(new Font("微软雅黑", 13f, System.Drawing.FontStyle.Bold), true);
            

            //设置列宽
            foreach (PdfGridColumn col in grid.Columns)
            {
                col.Width = 75f;
            }
           
            grid.Columns[2].Width = 150f;
            grid.Columns[3].Width = 150f;


            row.Cells[0].Value = "姓名";
            row.Cells[1].Value = "准考证号";
            row.Cells[2].Value = "职位名称";
            row.Cells[3].Value = "招录单位";
            row.Cells[4].Value = "抽签序号";

            row.Cells[2].ColumnSpan = 1;
            row.Cells[3].ColumnSpan = 1;
            row.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            row.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);

            for (int i = 0; i < tempList.Count; i++)
            {
                PdfGridRow rowTemp = grid.Rows.Add();

                rowTemp.Cells[0].Value = tempList[i].name;
                rowTemp.Cells[1].Value = tempList[i].admission_ticket_no;
                rowTemp.Cells[2].Value = tempList[i].job_name;
                rowTemp.Cells[3].Value = tempList[i].company;
                rowTemp.Cells[4].Value = tempList[i].no;

                rowTemp.Cells[2].ColumnSpan = 1;
                rowTemp.Cells[3].ColumnSpan = 1;

                rowTemp.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                rowTemp.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                rowTemp.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                rowTemp.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
                rowTemp.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);


            }



            //设置表格边框颜色、粗细
            PdfBorders borders = new PdfBorders
            {
                All = new PdfPen(System.Drawing.Color.Black, 0.1f)
            };
            foreach (PdfGridRow pgr in grid.Rows)
            {
                foreach (PdfGridCell pgc in pgr.Cells)
                {

                    pgc.Style.Borders = borders;
                }
            }



            PdfGridRow rowTemp2 = grid.Rows.Add();

            rowTemp2.Cells[0].Value = "签名:_____________________________________";

            rowTemp2.Cells[0].ColumnSpan = 3;
            rowTemp2.Cells[3].ColumnSpan = 2;


          
            rowTemp2.Cells[3].Value = "签字时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm");

            rowTemp2.Height = 100f;

            rowTemp2.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left,PdfVerticalAlignment.Middle);
            rowTemp2.Cells[0].Style.Borders = new PdfBorders
            {
                All = new PdfPen(System.Drawing.Color.White, 0)
            };

            rowTemp2.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
            rowTemp2.Cells[3].Style.Borders = new PdfBorders
            {
                All = new PdfPen(System.Drawing.Color.White, 0)
            };


            return grid;
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("123");
        }



        //创建页眉模板
        static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
        {
            //获取页面大小
            SizeF pageSize = doc.PageSettings.Size;

            //创建PdfPageTemplateElement对象headerSpace，即作页眉模板
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;

            //声明x,y两个float型变量
            float x = margins.Left;
            float y = 0;

            //在headerSpace中绘制图片
            PdfImage headerImage = PdfImage.FromFile(@"E:\\下载\\TOP-LOGO.png");
            float width = headerImage.Width / 3;
            float height = headerImage.Height / 3;
            headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);

            //在headerSpace中绘制线段
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

            //返回headerSpace
            return headerSpace;
        }

        // 创建页脚模板
        static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
        {
            //获取页面大小
            SizeF pageSize = doc.PageSettings.Size;

            //创建PdfPageTemplateElement对象footerSpace，即页脚模板
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;

            //声明x,y两个float型变量
            float x = margins.Left;
            float y = 0;

            //在footerSpace中绘制线段
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);

            //在footerSpace中绘制文字
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new System.Drawing.Font("微软雅黑", 10f), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "成都冰蓝科技有限公司\n电话：028-81705109\n网址：http://www.e-iceblue.cn";
            footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format);

            //在footerSpace中绘制当前页码和总页码
            PdfPageNumberField number = new PdfPageNumberField();
            PdfPageCountField count = new PdfPageCountField();
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "第{0}页/共{1}页", number, count);
            compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
            SizeF size = font.MeasureString(compositeField.Text);
            compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height);
            compositeField.Draw(footerSpace.Graphics);

            //返回footerSpace
            return footerSpace;
        }

    }
}
